<!doctype html>

<html>
<head>
  <link rel="shortcut icon" href="static/images/favicon.ico" type="image/x-icon">
  <title>directtransport.js (Closure Library API Documentation - JavaScript)</title>
  <link rel="stylesheet" href="static/css/base.css">
  <link rel="stylesheet" href="static/css/doc.css">
  <link rel="stylesheet" href="static/css/sidetree.css">
  <link rel="stylesheet" href="static/css/prettify.css">

  <script>
     var _staticFilePath = "static/";
     var _typeTreeName = "goog";
     var _fileTreeName = "Source";
  </script>

  <script src="static/js/doc.js">
  </script>


  <meta charset="utf8">
</head>

<body onload="grokdoc.onLoad();">

<div id="header">
  <div class="g-section g-tpl-50-50 g-split">
    <div class="g-unit g-first">
      <a id="logo" href="index.html">Closure Library API Documentation</a>
    </div>

    <div class="g-unit">
      <div class="g-c">
        <strong>Go to class or file:</strong>
        <input type="text" id="ac">
      </div>
    </div>
  </div>
</div>

<div class="clear"></div>

<h2><a href="local_closure_goog_net_xpc_directtransport.js.html">directtransport.js</a></h2>

<pre class="prettyprint lang-js">
<a name="line1"></a>// Copyright 2013 The Closure Library Authors. All Rights Reserved.
<a name="line2"></a>//
<a name="line3"></a>// Licensed under the Apache License, Version 2.0 (the &quot;License&quot;);
<a name="line4"></a>// you may not use this file except in compliance with the License.
<a name="line5"></a>// You may obtain a copy of the License at
<a name="line6"></a>//
<a name="line7"></a>//      http://www.apache.org/licenses/LICENSE-2.0
<a name="line8"></a>//
<a name="line9"></a>// Unless required by applicable law or agreed to in writing, software
<a name="line10"></a>// distributed under the License is distributed on an &quot;AS-IS&quot; BASIS,
<a name="line11"></a>// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
<a name="line12"></a>// See the License for the specific language governing permissions and
<a name="line13"></a>// limitations under the License.
<a name="line14"></a>
<a name="line15"></a>/**
<a name="line16"></a> * @fileoverview Provides an implementation of a transport that can call methods
<a name="line17"></a> * directly on a frame. Useful if you want to use XPC for crossdomain messaging
<a name="line18"></a> * (using another transport), or same domain messaging (using this transport).
<a name="line19"></a> */
<a name="line20"></a>
<a name="line21"></a>
<a name="line22"></a>goog.provide(&#39;goog.net.xpc.DirectTransport&#39;);
<a name="line23"></a>
<a name="line24"></a>goog.require(&#39;goog.Timer&#39;);
<a name="line25"></a>goog.require(&#39;goog.async.Deferred&#39;);
<a name="line26"></a>goog.require(&#39;goog.events.EventHandler&#39;);
<a name="line27"></a>goog.require(&#39;goog.log&#39;);
<a name="line28"></a>goog.require(&#39;goog.net.xpc&#39;);
<a name="line29"></a>goog.require(&#39;goog.net.xpc.CfgFields&#39;);
<a name="line30"></a>goog.require(&#39;goog.net.xpc.CrossPageChannelRole&#39;);
<a name="line31"></a>goog.require(&#39;goog.net.xpc.Transport&#39;);
<a name="line32"></a>goog.require(&#39;goog.net.xpc.TransportTypes&#39;);
<a name="line33"></a>goog.require(&#39;goog.object&#39;);
<a name="line34"></a>
<a name="line35"></a>
<a name="line36"></a>goog.scope(function() {
<a name="line37"></a>var CfgFields = goog.net.xpc.CfgFields;
<a name="line38"></a>var CrossPageChannelRole = goog.net.xpc.CrossPageChannelRole;
<a name="line39"></a>var Deferred = goog.async.Deferred;
<a name="line40"></a>var EventHandler = goog.events.EventHandler;
<a name="line41"></a>var Timer = goog.Timer;
<a name="line42"></a>var Transport = goog.net.xpc.Transport;
<a name="line43"></a>
<a name="line44"></a>
<a name="line45"></a>
<a name="line46"></a>/**
<a name="line47"></a> * A direct window to window method transport.
<a name="line48"></a> *
<a name="line49"></a> * If the windows are in the same security context, this transport calls
<a name="line50"></a> * directly into the other window without using any additional mechanism. This
<a name="line51"></a> * is mainly used in scenarios where you want to optionally use a cross domain
<a name="line52"></a> * transport in cross security context situations, or optionally use a direct
<a name="line53"></a> * transport in same security context situations.
<a name="line54"></a> *
<a name="line55"></a> * Note: Global properties are exported by using this transport. One to
<a name="line56"></a> * communicate with the other window by, currently crosswindowmessaging.channel,
<a name="line57"></a> * and by using goog.getUid on window, currently closure_uid_[0-9]+.
<a name="line58"></a> *
<a name="line59"></a> * @param {!goog.net.xpc.CrossPageChannel} channel The channel this
<a name="line60"></a> *     transport belongs to.
<a name="line61"></a> * @param {goog.dom.DomHelper=} opt_domHelper The dom helper to use for
<a name="line62"></a> *     finding the correct window/document. If omitted, uses the current
<a name="line63"></a> *     document.
<a name="line64"></a> * @constructor
<a name="line65"></a> * @extends {Transport}
<a name="line66"></a> */
<a name="line67"></a>goog.net.xpc.DirectTransport = function(channel, opt_domHelper) {
<a name="line68"></a>  goog.net.xpc.DirectTransport.base(this, &#39;constructor&#39;, opt_domHelper);
<a name="line69"></a>
<a name="line70"></a>  /**
<a name="line71"></a>   * The channel this transport belongs to.
<a name="line72"></a>   * @private {!goog.net.xpc.CrossPageChannel}
<a name="line73"></a>   */
<a name="line74"></a>  this.channel_ = channel;
<a name="line75"></a>
<a name="line76"></a>  /** @private {!EventHandler.&lt;!goog.net.xpc.DirectTransport&gt;} */
<a name="line77"></a>  this.eventHandler_ = new EventHandler(this);
<a name="line78"></a>  this.registerDisposable(this.eventHandler_);
<a name="line79"></a>
<a name="line80"></a>  /**
<a name="line81"></a>   * Timer for connection reattempts.
<a name="line82"></a>   * @private {!Timer}
<a name="line83"></a>   */
<a name="line84"></a>  this.maybeAttemptToConnectTimer_ = new Timer(
<a name="line85"></a>      DirectTransport.CONNECTION_ATTEMPT_INTERVAL_MS_,
<a name="line86"></a>      this.getWindow());
<a name="line87"></a>  this.registerDisposable(this.maybeAttemptToConnectTimer_);
<a name="line88"></a>
<a name="line89"></a>  /**
<a name="line90"></a>   * Fires once we&#39;ve received our SETUP_ACK message.
<a name="line91"></a>   * @private {!Deferred}
<a name="line92"></a>   */
<a name="line93"></a>  this.setupAckReceived_ = new Deferred();
<a name="line94"></a>
<a name="line95"></a>  /**
<a name="line96"></a>   * Fires once we&#39;ve sent our SETUP_ACK message.
<a name="line97"></a>   * @private {!Deferred}
<a name="line98"></a>   */
<a name="line99"></a>  this.setupAckSent_ = new Deferred();
<a name="line100"></a>
<a name="line101"></a>  /**
<a name="line102"></a>   * Fires once we&#39;re marked connected.
<a name="line103"></a>   * @private {!Deferred}
<a name="line104"></a>   */
<a name="line105"></a>  this.connected_ = new Deferred();
<a name="line106"></a>
<a name="line107"></a>  /**
<a name="line108"></a>   * The unique ID of this side of the connection. Used to determine when a peer
<a name="line109"></a>   * is reloaded.
<a name="line110"></a>   * @private {string}
<a name="line111"></a>   */
<a name="line112"></a>  this.endpointId_ = goog.net.xpc.getRandomString(10);
<a name="line113"></a>
<a name="line114"></a>  /**
<a name="line115"></a>   * The unique ID of the peer. If we get a message from a peer with an ID we
<a name="line116"></a>   * don&#39;t expect, we reset the connection.
<a name="line117"></a>   * @private {?string}
<a name="line118"></a>   */
<a name="line119"></a>  this.peerEndpointId_ = null;
<a name="line120"></a>
<a name="line121"></a>  /**
<a name="line122"></a>   * The map of sending messages.
<a name="line123"></a>   * @private {Object}
<a name="line124"></a>   */
<a name="line125"></a>  this.asyncSendsMap_ = {};
<a name="line126"></a>
<a name="line127"></a>  /**
<a name="line128"></a>   * The original channel name.
<a name="line129"></a>   * @private {string}
<a name="line130"></a>   */
<a name="line131"></a>  this.originalChannelName_ = this.channel_.name;
<a name="line132"></a>
<a name="line133"></a>  // We reconfigure the channel name to include the role so that we can
<a name="line134"></a>  // communicate in the same window between the different roles on the
<a name="line135"></a>  // same channel.
<a name="line136"></a>  this.channel_.updateChannelNameAndCatalog(
<a name="line137"></a>      DirectTransport.getRoledChannelName_(this.channel_.name,
<a name="line138"></a>                                           this.channel_.getRole()));
<a name="line139"></a>
<a name="line140"></a>  /**
<a name="line141"></a>   * Flag indicating if this instance of the transport has been initialized.
<a name="line142"></a>   * @private {boolean}
<a name="line143"></a>   */
<a name="line144"></a>  this.initialized_ = false;
<a name="line145"></a>
<a name="line146"></a>  // We don&#39;t want to mark ourselves connected until we have sent whatever
<a name="line147"></a>  // message will cause our counterpart in the other frame to also declare
<a name="line148"></a>  // itself connected, if there is such a message.  Otherwise we risk a user
<a name="line149"></a>  // message being sent in advance of that message, and it being discarded.
<a name="line150"></a>
<a name="line151"></a>  // Two sided handshake:
<a name="line152"></a>  // SETUP_ACK has to have been received, and sent.
<a name="line153"></a>  this.connected_.awaitDeferred(this.setupAckReceived_);
<a name="line154"></a>  this.connected_.awaitDeferred(this.setupAckSent_);
<a name="line155"></a>
<a name="line156"></a>  this.connected_.addCallback(this.notifyConnected_, this);
<a name="line157"></a>  this.connected_.callback(true);
<a name="line158"></a>
<a name="line159"></a>  this.eventHandler_.
<a name="line160"></a>      listen(this.maybeAttemptToConnectTimer_, Timer.TICK,
<a name="line161"></a>          this.maybeAttemptToConnect_);
<a name="line162"></a>
<a name="line163"></a>  goog.log.info(
<a name="line164"></a>      goog.net.xpc.logger,
<a name="line165"></a>      &#39;DirectTransport created. role=&#39; + this.channel_.getRole());
<a name="line166"></a>};
<a name="line167"></a>goog.inherits(goog.net.xpc.DirectTransport, Transport);
<a name="line168"></a>var DirectTransport = goog.net.xpc.DirectTransport;
<a name="line169"></a>
<a name="line170"></a>
<a name="line171"></a>/**
<a name="line172"></a> * @private {number}
<a name="line173"></a> * @const
<a name="line174"></a> */
<a name="line175"></a>DirectTransport.CONNECTION_ATTEMPT_INTERVAL_MS_ = 100;
<a name="line176"></a>
<a name="line177"></a>
<a name="line178"></a>/**
<a name="line179"></a> * The delay to notify the xpc of a successful connection. This is used
<a name="line180"></a> * to allow both parties to be connected if one party&#39;s connection callback
<a name="line181"></a> * invokes an immediate send.
<a name="line182"></a> * @private {number}
<a name="line183"></a> * @const
<a name="line184"></a> */
<a name="line185"></a>DirectTransport.CONNECTION_DELAY_INTERVAL_MS_ = 0;
<a name="line186"></a>
<a name="line187"></a>
<a name="line188"></a>/**
<a name="line189"></a> * @param {!Window} peerWindow The peer window to check if DirectTranport is
<a name="line190"></a> *     supported on.
<a name="line191"></a> * @return {boolean} Whether this transport is supported.
<a name="line192"></a> */
<a name="line193"></a>DirectTransport.isSupported = function(peerWindow) {
<a name="line194"></a>  /** @preserveTry */
<a name="line195"></a>  try {
<a name="line196"></a>    return window.document.domain == peerWindow.document.domain;
<a name="line197"></a>  } catch (e) {
<a name="line198"></a>    return false;
<a name="line199"></a>  }
<a name="line200"></a>};
<a name="line201"></a>
<a name="line202"></a>
<a name="line203"></a>/**
<a name="line204"></a> * Tracks the number of DirectTransport channels that have been
<a name="line205"></a> * initialized but not disposed yet in a map keyed by the UID of the window
<a name="line206"></a> * object.  This allows for multiple windows to be initiallized and listening
<a name="line207"></a> * for messages.
<a name="line208"></a> * @private {!Object.&lt;number&gt;}
<a name="line209"></a> */
<a name="line210"></a>DirectTransport.activeCount_ = {};
<a name="line211"></a>
<a name="line212"></a>
<a name="line213"></a>/**
<a name="line214"></a> * Path of global message proxy.
<a name="line215"></a> * @private {string}
<a name="line216"></a> * @const
<a name="line217"></a> */
<a name="line218"></a>// TODO(user): Make this configurable using the CfgFields.
<a name="line219"></a>DirectTransport.GLOBAL_TRANPORT_PATH_ = &#39;crosswindowmessaging.channel&#39;;
<a name="line220"></a>
<a name="line221"></a>
<a name="line222"></a>/**
<a name="line223"></a> * The delimiter used for transport service messages.
<a name="line224"></a> * @private {string}
<a name="line225"></a> * @const
<a name="line226"></a> */
<a name="line227"></a>DirectTransport.MESSAGE_DELIMITER_ = &#39;,&#39;;
<a name="line228"></a>
<a name="line229"></a>
<a name="line230"></a>/**
<a name="line231"></a> * Initializes this transport. Registers a method for &#39;message&#39;-events in the
<a name="line232"></a> * global scope.
<a name="line233"></a> * @param {!Window} listenWindow The window to listen to events on.
<a name="line234"></a> * @private
<a name="line235"></a> */
<a name="line236"></a>DirectTransport.initialize_ = function(listenWindow) {
<a name="line237"></a>  var uid = goog.getUid(listenWindow);
<a name="line238"></a>  var value = DirectTransport.activeCount_[uid] || 0;
<a name="line239"></a>  if (value == 0) {
<a name="line240"></a>    // Set up a handler on the window to proxy messages to class.
<a name="line241"></a>    var globalProxy = goog.getObjectByName(
<a name="line242"></a>        DirectTransport.GLOBAL_TRANPORT_PATH_,
<a name="line243"></a>        listenWindow);
<a name="line244"></a>    if (globalProxy == null) {
<a name="line245"></a>      goog.exportSymbol(
<a name="line246"></a>          DirectTransport.GLOBAL_TRANPORT_PATH_,
<a name="line247"></a>          DirectTransport.messageReceivedHandler_,
<a name="line248"></a>          listenWindow);
<a name="line249"></a>    }
<a name="line250"></a>  }
<a name="line251"></a>  DirectTransport.activeCount_[uid]++;
<a name="line252"></a>};
<a name="line253"></a>
<a name="line254"></a>
<a name="line255"></a>/**
<a name="line256"></a> * @param {string} channelName The channel name.
<a name="line257"></a> * @param {string|number} role The role.
<a name="line258"></a> * @return {string} The formatted channel name including role.
<a name="line259"></a> * @private
<a name="line260"></a> */
<a name="line261"></a>DirectTransport.getRoledChannelName_ = function(channelName, role) {
<a name="line262"></a>  return channelName + &#39;_&#39; + role;
<a name="line263"></a>};
<a name="line264"></a>
<a name="line265"></a>
<a name="line266"></a>/**
<a name="line267"></a> * @param {!Object} literal The literal unrenamed message.
<a name="line268"></a> * @return {boolean} Whether the message was successfully delivered to a
<a name="line269"></a> *     channel.
<a name="line270"></a> * @private
<a name="line271"></a> */
<a name="line272"></a>DirectTransport.messageReceivedHandler_ = function(literal) {
<a name="line273"></a>  var msg = DirectTransport.Message_.fromLiteral(literal);
<a name="line274"></a>
<a name="line275"></a>  var channelName = msg.channelName;
<a name="line276"></a>  var service = msg.service;
<a name="line277"></a>  var payload = msg.payload;
<a name="line278"></a>
<a name="line279"></a>  goog.log.fine(goog.net.xpc.logger,
<a name="line280"></a>      &#39;messageReceived: channel=&#39; + channelName +
<a name="line281"></a>      &#39;, service=&#39; + service + &#39;, payload=&#39; + payload);
<a name="line282"></a>
<a name="line283"></a>  // Attempt to deliver message to the channel. Keep in mind that it may not
<a name="line284"></a>  // exist for several reasons, including but not limited to:
<a name="line285"></a>  //  - a malformed message
<a name="line286"></a>  //  - the channel simply has not been created
<a name="line287"></a>  //  - channel was created in a different namespace
<a name="line288"></a>  //  - message was sent to the wrong window
<a name="line289"></a>  //  - channel has become stale (e.g. caching iframes and back clicks)
<a name="line290"></a>  var channel = goog.net.xpc.channels[channelName];
<a name="line291"></a>  if (channel) {
<a name="line292"></a>    channel.xpcDeliver(service, payload);
<a name="line293"></a>    return true;
<a name="line294"></a>  }
<a name="line295"></a>
<a name="line296"></a>  var transportMessageType = DirectTransport.parseTransportPayload_(payload)[0];
<a name="line297"></a>
<a name="line298"></a>  // Check if there are any stale channel names that can be updated.
<a name="line299"></a>  for (var staleChannelName in goog.net.xpc.channels) {
<a name="line300"></a>    var staleChannel = goog.net.xpc.channels[staleChannelName];
<a name="line301"></a>    if (staleChannel.getRole() == CrossPageChannelRole.INNER &amp;&amp;
<a name="line302"></a>        !staleChannel.isConnected() &amp;&amp;
<a name="line303"></a>        service == goog.net.xpc.TRANSPORT_SERVICE_ &amp;&amp;
<a name="line304"></a>        transportMessageType == goog.net.xpc.SETUP) {
<a name="line305"></a>      // Inner peer received SETUP message but channel names did not match.
<a name="line306"></a>      // Start using the channel name sent from outer peer. The channel name
<a name="line307"></a>      // of the inner peer can easily become out of date, as iframe&#39;s and their
<a name="line308"></a>      // JS state get cached in many browsers upon page reload or history
<a name="line309"></a>      // navigation (particularly Firefox 1.5+).
<a name="line310"></a>      staleChannel.updateChannelNameAndCatalog(channelName);
<a name="line311"></a>      staleChannel.xpcDeliver(service, payload);
<a name="line312"></a>      return true;
<a name="line313"></a>    }
<a name="line314"></a>  }
<a name="line315"></a>
<a name="line316"></a>  // Failed to find a channel to deliver this message to, so simply ignore it.
<a name="line317"></a>  goog.log.info(goog.net.xpc.logger, &#39;channel name mismatch; message ignored.&#39;);
<a name="line318"></a>  return false;
<a name="line319"></a>};
<a name="line320"></a>
<a name="line321"></a>
<a name="line322"></a>/**
<a name="line323"></a> * The transport type.
<a name="line324"></a> * @type {number}
<a name="line325"></a> * @override
<a name="line326"></a> */
<a name="line327"></a>DirectTransport.prototype.transportType = goog.net.xpc.TransportTypes.DIRECT;
<a name="line328"></a>
<a name="line329"></a>
<a name="line330"></a>/**
<a name="line331"></a> * Handles transport service messages.
<a name="line332"></a> * @param {string} payload The message content.
<a name="line333"></a> * @override
<a name="line334"></a> */
<a name="line335"></a>DirectTransport.prototype.transportServiceHandler = function(payload) {
<a name="line336"></a>  var transportParts = DirectTransport.parseTransportPayload_(payload);
<a name="line337"></a>  var transportMessageType = transportParts[0];
<a name="line338"></a>  var peerEndpointId = transportParts[1];
<a name="line339"></a>  switch (transportMessageType) {
<a name="line340"></a>    case goog.net.xpc.SETUP_ACK_:
<a name="line341"></a>      if (!this.setupAckReceived_.hasFired()) {
<a name="line342"></a>        this.setupAckReceived_.callback(true);
<a name="line343"></a>      }
<a name="line344"></a>      break;
<a name="line345"></a>    case goog.net.xpc.SETUP:
<a name="line346"></a>      this.sendSetupAckMessage_();
<a name="line347"></a>      if ((this.peerEndpointId_ != null) &amp;&amp;
<a name="line348"></a>          (this.peerEndpointId_ != peerEndpointId)) {
<a name="line349"></a>        // Send a new SETUP message since the peer has been replaced.
<a name="line350"></a>        goog.log.info(goog.net.xpc.logger,
<a name="line351"></a>            &#39;Sending SETUP and changing peer ID to: &#39; + peerEndpointId);
<a name="line352"></a>        this.sendSetupMessage_();
<a name="line353"></a>      }
<a name="line354"></a>      this.peerEndpointId_ = peerEndpointId;
<a name="line355"></a>      break;
<a name="line356"></a>  }
<a name="line357"></a>};
<a name="line358"></a>
<a name="line359"></a>
<a name="line360"></a>/**
<a name="line361"></a> * Sends a SETUP transport service message.
<a name="line362"></a> * @private
<a name="line363"></a> */
<a name="line364"></a>DirectTransport.prototype.sendSetupMessage_ = function() {
<a name="line365"></a>  // Although we could send real objects, since some other transports are
<a name="line366"></a>  // limited to strings we also keep this requirement.
<a name="line367"></a>  var payload = goog.net.xpc.SETUP;
<a name="line368"></a>  payload += DirectTransport.MESSAGE_DELIMITER_;
<a name="line369"></a>  payload += this.endpointId_;
<a name="line370"></a>  this.send(goog.net.xpc.TRANSPORT_SERVICE_, payload);
<a name="line371"></a>};
<a name="line372"></a>
<a name="line373"></a>
<a name="line374"></a>/**
<a name="line375"></a> * Sends a SETUP_ACK transport service message.
<a name="line376"></a> * @private
<a name="line377"></a> */
<a name="line378"></a>DirectTransport.prototype.sendSetupAckMessage_ = function() {
<a name="line379"></a>  this.send(goog.net.xpc.TRANSPORT_SERVICE_, goog.net.xpc.SETUP_ACK_);
<a name="line380"></a>  if (!this.setupAckSent_.hasFired()) {
<a name="line381"></a>    this.setupAckSent_.callback(true);
<a name="line382"></a>  }
<a name="line383"></a>};
<a name="line384"></a>
<a name="line385"></a>
<a name="line386"></a>/** @override */
<a name="line387"></a>DirectTransport.prototype.connect = function() {
<a name="line388"></a>  var win = this.getWindow();
<a name="line389"></a>  if (win) {
<a name="line390"></a>    DirectTransport.initialize_(win);
<a name="line391"></a>    this.initialized_ = true;
<a name="line392"></a>    this.maybeAttemptToConnect_();
<a name="line393"></a>  } else {
<a name="line394"></a>    goog.log.fine(goog.net.xpc.logger, &#39;connect(): no window to initialize.&#39;);
<a name="line395"></a>  }
<a name="line396"></a>};
<a name="line397"></a>
<a name="line398"></a>
<a name="line399"></a>/**
<a name="line400"></a> * Connects to other peer. In the case of the outer peer, the setup messages are
<a name="line401"></a> * likely sent before the inner peer is ready to receive them. Therefore, this
<a name="line402"></a> * function will continue trying to send the SETUP message until the inner peer
<a name="line403"></a> * responds. In the case of the inner peer, it will occasionally have its
<a name="line404"></a> * channel name fall out of sync with the outer peer, particularly during
<a name="line405"></a> * soft-reloads and history navigations.
<a name="line406"></a> * @private
<a name="line407"></a> */
<a name="line408"></a>DirectTransport.prototype.maybeAttemptToConnect_ = function() {
<a name="line409"></a>  var outerRole = this.channel_.getRole() == CrossPageChannelRole.OUTER;
<a name="line410"></a>  if (this.channel_.isConnected()) {
<a name="line411"></a>    this.maybeAttemptToConnectTimer_.stop();
<a name="line412"></a>    return;
<a name="line413"></a>  }
<a name="line414"></a>  this.maybeAttemptToConnectTimer_.start();
<a name="line415"></a>  this.sendSetupMessage_();
<a name="line416"></a>};
<a name="line417"></a>
<a name="line418"></a>
<a name="line419"></a>/**
<a name="line420"></a> * Prepares to send a message.
<a name="line421"></a> * @param {string} service The name of the service the message is to be
<a name="line422"></a> *     delivered to.
<a name="line423"></a> * @param {string} payload The message content.
<a name="line424"></a> * @override
<a name="line425"></a> */
<a name="line426"></a>DirectTransport.prototype.send = function(service, payload) {
<a name="line427"></a>  if (!this.channel_.getPeerWindowObject()) {
<a name="line428"></a>    goog.log.fine(goog.net.xpc.logger, &#39;send(): window not ready&#39;);
<a name="line429"></a>    return;
<a name="line430"></a>  }
<a name="line431"></a>  var channelName = DirectTransport.getRoledChannelName_(
<a name="line432"></a>      this.originalChannelName_,
<a name="line433"></a>      this.getPeerRole_());
<a name="line434"></a>
<a name="line435"></a>  var message = new DirectTransport.Message_(
<a name="line436"></a>      channelName,
<a name="line437"></a>      service,
<a name="line438"></a>      payload);
<a name="line439"></a>
<a name="line440"></a>  if (this.channel_.getConfig()[CfgFields.DIRECT_TRANSPORT_SYNC_MODE]) {
<a name="line441"></a>    this.executeScheduledSend_(message);
<a name="line442"></a>  } else {
<a name="line443"></a>    // Note: goog.async.nextTick doesn&#39;t support cancelling or disposal so
<a name="line444"></a>    // leaving as 0ms timer, though this may have performance implications.
<a name="line445"></a>    this.asyncSendsMap_[goog.getUid(message)] =
<a name="line446"></a>        Timer.callOnce(goog.bind(this.executeScheduledSend_, this, message), 0);
<a name="line447"></a>  }
<a name="line448"></a>};
<a name="line449"></a>
<a name="line450"></a>
<a name="line451"></a>/**
<a name="line452"></a> * Sends the message.
<a name="line453"></a> * @param {!DirectTransport.Message_} message The message to send.
<a name="line454"></a> * @private
<a name="line455"></a> */
<a name="line456"></a>DirectTransport.prototype.executeScheduledSend_ = function(message) {
<a name="line457"></a>  var messageId = goog.getUid(message);
<a name="line458"></a>  if (this.asyncSendsMap_[messageId]) {
<a name="line459"></a>    delete this.asyncSendsMap_[messageId];
<a name="line460"></a>  }
<a name="line461"></a>
<a name="line462"></a>  /** @preserveTry */
<a name="line463"></a>  try {
<a name="line464"></a>    var peerProxy = goog.getObjectByName(
<a name="line465"></a>        DirectTransport.GLOBAL_TRANPORT_PATH_,
<a name="line466"></a>        this.channel_.getPeerWindowObject());
<a name="line467"></a>  } catch (error) {
<a name="line468"></a>    goog.log.warning(
<a name="line469"></a>        goog.net.xpc.logger,
<a name="line470"></a>        &#39;Can\&#39;t access other window, ignoring.&#39;,
<a name="line471"></a>        error);
<a name="line472"></a>    return;
<a name="line473"></a>  }
<a name="line474"></a>
<a name="line475"></a>  if (goog.isNull(peerProxy)) {
<a name="line476"></a>    goog.log.warning(
<a name="line477"></a>        goog.net.xpc.logger,
<a name="line478"></a>        &#39;Peer window had no global function.&#39;);
<a name="line479"></a>    return;
<a name="line480"></a>  }
<a name="line481"></a>
<a name="line482"></a>  /** @preserveTry */
<a name="line483"></a>  try {
<a name="line484"></a>    peerProxy(message.toLiteral());
<a name="line485"></a>    goog.log.info(
<a name="line486"></a>        goog.net.xpc.logger,
<a name="line487"></a>        &#39;send(): channelName=&#39; + message.channelName +
<a name="line488"></a>        &#39; service=&#39; + message.service +
<a name="line489"></a>        &#39; payload=&#39; + message.payload);
<a name="line490"></a>  } catch (error) {
<a name="line491"></a>    goog.log.warning(
<a name="line492"></a>        goog.net.xpc.logger,
<a name="line493"></a>        &#39;Error performing call, ignoring.&#39;,
<a name="line494"></a>        error);
<a name="line495"></a>  }
<a name="line496"></a>};
<a name="line497"></a>
<a name="line498"></a>
<a name="line499"></a>/**
<a name="line500"></a> * @return {goog.net.xpc.CrossPageChannelRole} The role of peer channel (either
<a name="line501"></a> *     inner or outer).
<a name="line502"></a> * @private
<a name="line503"></a> */
<a name="line504"></a>DirectTransport.prototype.getPeerRole_ = function() {
<a name="line505"></a>  var role = this.channel_.getRole();
<a name="line506"></a>  return role == goog.net.xpc.CrossPageChannelRole.OUTER ?
<a name="line507"></a>      goog.net.xpc.CrossPageChannelRole.INNER :
<a name="line508"></a>      goog.net.xpc.CrossPageChannelRole.OUTER;
<a name="line509"></a>};
<a name="line510"></a>
<a name="line511"></a>
<a name="line512"></a>/**
<a name="line513"></a> * Notifies the channel that this transport is connected.
<a name="line514"></a> * @private
<a name="line515"></a> */
<a name="line516"></a>DirectTransport.prototype.notifyConnected_ = function() {
<a name="line517"></a>  // Add a delay as the connection callback will break if this transport is
<a name="line518"></a>  // synchronous and the callback invokes send() immediately.
<a name="line519"></a>  this.channel_.notifyConnected(
<a name="line520"></a>      this.channel_.getConfig()[CfgFields.DIRECT_TRANSPORT_SYNC_MODE] ?
<a name="line521"></a>      DirectTransport.CONNECTION_DELAY_INTERVAL_MS_ : 0);
<a name="line522"></a>};
<a name="line523"></a>
<a name="line524"></a>
<a name="line525"></a>/** @override */
<a name="line526"></a>DirectTransport.prototype.disposeInternal = function() {
<a name="line527"></a>  if (this.initialized_) {
<a name="line528"></a>    var listenWindow = this.getWindow();
<a name="line529"></a>    var uid = goog.getUid(listenWindow);
<a name="line530"></a>    var value = --DirectTransport.activeCount_[uid];
<a name="line531"></a>    if (value == 1) {
<a name="line532"></a>      goog.exportSymbol(
<a name="line533"></a>          DirectTransport.GLOBAL_TRANPORT_PATH_,
<a name="line534"></a>          null,
<a name="line535"></a>          listenWindow);
<a name="line536"></a>    }
<a name="line537"></a>  }
<a name="line538"></a>
<a name="line539"></a>  if (this.asyncSendsMap_) {
<a name="line540"></a>    goog.object.forEach(this.asyncSendsMap_, function(timerId) {
<a name="line541"></a>      Timer.clear(timerId);
<a name="line542"></a>    });
<a name="line543"></a>    this.asyncSendsMap_ = null;
<a name="line544"></a>  }
<a name="line545"></a>
<a name="line546"></a>  // Deferred&#39;s aren&#39;t disposables.
<a name="line547"></a>  if (this.setupAckReceived_) {
<a name="line548"></a>    this.setupAckReceived_.cancel();
<a name="line549"></a>    delete this.setupAckReceived_;
<a name="line550"></a>  }
<a name="line551"></a>  if (this.setupAckSent_) {
<a name="line552"></a>    this.setupAckSent_.cancel();
<a name="line553"></a>    delete this.setupAckSent_;
<a name="line554"></a>  }
<a name="line555"></a>  if (this.connected_) {
<a name="line556"></a>    this.connected_.cancel();
<a name="line557"></a>    delete this.connected_;
<a name="line558"></a>  }
<a name="line559"></a>
<a name="line560"></a>  DirectTransport.base(this, &#39;disposeInternal&#39;);
<a name="line561"></a>};
<a name="line562"></a>
<a name="line563"></a>
<a name="line564"></a>/**
<a name="line565"></a> * Parses a transport service payload message.
<a name="line566"></a> * @param {string} payload The payload.
<a name="line567"></a> * @return {!Array.&lt;?string&gt;} An array with the message type as the first member
<a name="line568"></a> *     and the endpoint id as the second, if one was sent, or null otherwise.
<a name="line569"></a> * @private
<a name="line570"></a> */
<a name="line571"></a>DirectTransport.parseTransportPayload_ = function(payload) {
<a name="line572"></a>  var transportParts = /** @type {!Array.&lt;?string&gt;} */ (payload.split(
<a name="line573"></a>      DirectTransport.MESSAGE_DELIMITER_));
<a name="line574"></a>  transportParts[1] = transportParts[1] || null; // Usually endpointId.
<a name="line575"></a>  return transportParts;
<a name="line576"></a>};
<a name="line577"></a>
<a name="line578"></a>
<a name="line579"></a>
<a name="line580"></a>/**
<a name="line581"></a> * Message container that gets passed back and forth between windows.
<a name="line582"></a> * @param {string} channelName The channel name to tranport messages on.
<a name="line583"></a> * @param {string} service The service to send the payload to.
<a name="line584"></a> * @param {string} payload The payload to send.
<a name="line585"></a> * @constructor
<a name="line586"></a> * @struct
<a name="line587"></a> * @private
<a name="line588"></a> */
<a name="line589"></a>DirectTransport.Message_ = function(channelName, service, payload) {
<a name="line590"></a>  /**
<a name="line591"></a>   * The name of the channel.
<a name="line592"></a>   * @type {string}
<a name="line593"></a>   */
<a name="line594"></a>  this.channelName = channelName;
<a name="line595"></a>
<a name="line596"></a>  /**
<a name="line597"></a>   * The service on the channel.
<a name="line598"></a>   * @type {string}
<a name="line599"></a>   */
<a name="line600"></a>  this.service = service;
<a name="line601"></a>
<a name="line602"></a>  /**
<a name="line603"></a>   * The payload.
<a name="line604"></a>   * @type {string}
<a name="line605"></a>   */
<a name="line606"></a>  this.payload = payload;
<a name="line607"></a>};
<a name="line608"></a>
<a name="line609"></a>
<a name="line610"></a>/**
<a name="line611"></a> * Converts a message to a literal object.
<a name="line612"></a> * @return {!Object} The message as a literal object.
<a name="line613"></a> */
<a name="line614"></a>DirectTransport.Message_.prototype.toLiteral = function() {
<a name="line615"></a>  return {
<a name="line616"></a>    &#39;channelName&#39;: this.channelName,
<a name="line617"></a>    &#39;service&#39;: this.service,
<a name="line618"></a>    &#39;payload&#39;: this.payload
<a name="line619"></a>  };
<a name="line620"></a>};
<a name="line621"></a>
<a name="line622"></a>
<a name="line623"></a>/**
<a name="line624"></a> * Creates a Message_ from a literal object.
<a name="line625"></a> * @param {!Object} literal The literal to convert to Message.
<a name="line626"></a> * @return {!DirectTransport.Message_} The Message.
<a name="line627"></a> */
<a name="line628"></a>DirectTransport.Message_.fromLiteral = function(literal) {
<a name="line629"></a>  return new DirectTransport.Message_(
<a name="line630"></a>      literal[&#39;channelName&#39;],
<a name="line631"></a>      literal[&#39;service&#39;],
<a name="line632"></a>      literal[&#39;payload&#39;]);
<a name="line633"></a>};
<a name="line634"></a>
<a name="line635"></a>});  // goog.scope
</pre>


</body>
</html>
